package com.yonglian.system.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.yonglian.common.config.YonglianCMSConfig;
import com.yonglian.common.exception.file.InvalidExtensionException;
import com.yonglian.common.utils.DateUtils;
import com.yonglian.common.utils.ShiroUtils;
import com.yonglian.common.utils.file.FileUploadUtils;
import com.yonglian.common.utils.file.MimeTypeUtils;
import com.yonglian.system.mapper.CmsActivityStandardMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yonglian.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.yonglian.system.domain.CmsActivityStandard;
import com.yonglian.system.mapper.CmsActivityMapper;
import com.yonglian.system.domain.CmsActivity;
import com.yonglian.system.service.ICmsActivityService;
import com.yonglian.common.core.text.Convert;

/**
 * 活动Service业务层处理
 *
 * @author gyok
 * @date 2024-05-28
 */
@Service
public class CmsActivityServiceImpl implements ICmsActivityService
{
    @Autowired
    private CmsActivityMapper cmsActivityMapper;

    @Autowired
    private CmsActivityStandardMapper cmsActivityStandardMapper;

    /**
     * 查询活动
     *
     * @param id 活动主键
     * @return 活动
     */
    @Override
    public CmsActivity selectCmsActivityById(Long id)
    {
        return cmsActivityMapper.selectCmsActivityById(id);
    }

    /**
     * 查询活动列表
     *
     * @param cmsActivity 活动
     * @return 活动
     */
    @Override
    public List<CmsActivity> selectCmsActivityList(CmsActivity cmsActivity)
    {
        return cmsActivityMapper.selectCmsActivityList(cmsActivity);
    }

    /**
     * 新增活动
     *
     * @param cmsActivity 活动
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCmsActivity(CmsActivity cmsActivity)
    {
        processImages(cmsActivity);
        cmsActivity.setCreateTime(DateUtils.getNowDate());
        cmsActivity.setCreateBy(ShiroUtils.getSysUser().getUserName());
        int rows = cmsActivityMapper.insertCmsActivity(cmsActivity);
        insertCmsActivityStandard(cmsActivity);
        return rows;
    }

    /**
     * 修改活动
     *
     * @param cmsActivity 活动
     * @return 结果
     */
    @Transactional
    @Override
    public int updateCmsActivity(CmsActivity cmsActivity)
    {
        processImages(cmsActivity);
        cmsActivityStandardMapper.deleteCmsActivityStandardByActivityId(cmsActivity.getId());
        insertCmsActivityStandard(cmsActivity);
        return cmsActivityMapper.updateCmsActivity(cmsActivity);
    }

    /**
     * 批量删除活动
     *
     * @param ids 需要删除的活动主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCmsActivityByIds(String ids)
    {
        cmsActivityStandardMapper.deleteCmsActivityStandardByActivityIds(Convert.toStrArray(ids));
        return cmsActivityMapper.deleteCmsActivityByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除活动信息
     *
     * @param id 活动主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCmsActivityById(Long id)
    {
        cmsActivityStandardMapper.deleteCmsActivityStandardByActivityId(id);
        return cmsActivityMapper.deleteCmsActivityById(id);
    }

    /**
     * 新增活动标准信息
     *
     * @param cmsActivity 活动对象
     */
    public void insertCmsActivityStandard(CmsActivity cmsActivity)
    {
        List<CmsActivityStandard> cmsActivityStandardList = cmsActivity.getCmsActivityStandardList();
        Long id = cmsActivity.getId();
        if (StringUtils.isNotNull(cmsActivityStandardList))
        {
            List<CmsActivityStandard> list = new ArrayList<CmsActivityStandard>();
            for (CmsActivityStandard cmsActivityStandard : cmsActivityStandardList)
            {
                cmsActivityStandard.setActivityId(id);
                list.add(cmsActivityStandard);
            }
            if (list.size() > 0)
            {
                cmsActivityStandardMapper.batchCmsActivityStandard(list);
            }
        }
    }

    /**
     * 处理活动图片
     * @param activity
     */
    public void processImages(CmsActivity activity) {
        // 封面
        if (activity.getCoverFile() != null && !activity.getCoverFile().isEmpty()) {
            try {
                String imgPath = FileUploadUtils.upload(YonglianCMSConfig.getUploadPath(), activity.getCoverFile(), MimeTypeUtils.IMAGE_EXTENSION);
                activity.setCover(imgPath);
            } catch (IOException e) {
                throw new RuntimeException(e);
            } catch (InvalidExtensionException e) {
                throw new RuntimeException(e);
            }
        }

        // 活动图片
        if (activity.getActivityPictureFiles() != null && !activity.getActivityPictureFiles().isEmpty()
                && !activity.getActivityPictureFiles().get(0).isEmpty()) {
            String[] activityPictures = activity.getActivityPictureFiles().stream()
                    .map(file -> {
                        try {
                            return FileUploadUtils.upload(YonglianCMSConfig.getUploadPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        } catch (InvalidExtensionException e) {
                            throw new RuntimeException(e);
                        }
                    })
                    .toArray(String[]::new);
            activity.setActivityPictures(String.join("|", activityPictures));
        }
    }
}
